package logic

import (
	"beike/houserpc/houserpc"
	"beike/houserpc/internal/svc"
	"beike/models"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type HouseAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHouseAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseAddLogic {
	return &HouseAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HouseAddLogic) HouseAdd(in *houserpc.HouseAddRequest) (*houserpc.HouseAddResponse, error) {
	// todo: add your logic here and delete this line

	if in.Title == "" || in.Intro == "" || in.Price == 0 || in.Address == "" {
		return nil, fmt.Errorf("房源信息还未完善，请在完善后上传")
	}

	house := models.House{
		Title:   in.Title,
		Intro:   in.Intro,
		Price:   float64(in.Price),
		Address: in.Address,
		Types:   in.Types,
		Area:    float64(in.Area),
		UserId:  in.UserId,
	}

	models.DB.Debug().Create(&house)

	return &houserpc.HouseAddResponse{
		HouseId: int64(house.ID),
	}, nil
}
